$.ajax({
    type: 'get',
    url: '/categories',
    success: function (response) {
        var html = template('categoriesTpl', { data: response });
        $('#categoriesBox').html(html);
    }
});
$('#addCategory').on('submit', function () {
    var formData = $(this).serialize();
    $.ajax({
        type: 'post',
        url: '/categories',
        data: formData,
        success: function () {
            location.reload()
        }
    })
});
$('#categoriesBox').on('click', '.edit', function () {
    var id = $(this).attr('data-id');
    $.ajax({
        type: 'put',
        url: '/categories/' + id,
        success: function (response) {
            var html = template('modifyCategoriesTpl', response);
            $('#cBox').html(html);
        }
    })
});
$('#cBox').on('submit', '#modifyCategory', function () {
    var id = $(this).attr('data-id');
    var formData = $(this).serialize();
    $.ajax({
        type: 'put',
        url: '/categories/' + id,
        data: formData,
        success: function () {
            location.reload()
        }
    });
    return false
});
$('#categoriesBox').on('click', '.delete', function () {
    var id = $(this).attr('data-id');
    if (confirm('您确定要删除该分类吗？')) {
        $.ajax({
            type: 'delete',
            url: '/categories/' + id,
            success: function () {
                location.reload();
            }
        })
    }
});
